<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:xpdl="http://www.wfmc.org/2008/XPDL2.1" xmlns:grupo5="http://grupo5">
	<xsl:output method="xml"  indent="yes"/>	

	<xsl:variable name="groupv" select="//xpdl:Activity[@grupo5:tipo='group']/xpdl:Description/text()">
	</xsl:variable>
	
	<xsl:template name="groupWorkflow" >
		<xsl:for-each select="//xpdl:WorkflowProcess[@grupo5:identificador]">
			<workflow  name="untitled" version="1.0.0" vistrail_id="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vistrails.org/workflow.xsd">
			<xsl:attribute name="id"><xsl:value-of select="@Id"></xsl:value-of> </xsl:attribute>
			<xsl:apply-templates select="xpdl:Transitions/xpdl:Transition"/>
			<xsl:apply-templates select="xpdl:ExtendedAttributes/xpdl:ExtendedAttribute"/>
			<xsl:apply-templates select="xpdl:Activities/xpdl:Activity"/>
			</workflow>
		</xsl:for-each>
	</xsl:template>

	
	<!-- sobrescrevendo a regra default para que não seja impresso texto indesejável -->
	<xsl:template match="text()"/>
	
	<xsl:template match="*">
	</xsl:template>


	<!--regra para iniciar o processamento do documento xpdl  -->
	<xsl:template match="/xpdl:Package">
			<xsl:apply-templates/>
	</xsl:template>
	
	<!-- -->
	<xsl:template match="xpdl:WorkflowProcesses">
		<xsl:apply-templates select="xpdl:WorkflowProcess[@grupo5:tipo='nada']"></xsl:apply-templates>
	</xsl:template>


	<xsl:template match="xpdl:WorkflowProcess[@grupo5:tipo='nada']">
		<workflow  name="untitled" version="1.0.0" vistrail_id="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vistrails.org/workflow.xsd">
			<xsl:attribute name="id"><xsl:value-of select="@Id"></xsl:value-of> </xsl:attribute>
			<xsl:apply-templates select=".//xpdl:Transition" />
			<xsl:apply-templates select=".//xpdl:ExtendedAttribute" />
			<xsl:apply-templates select=".//xpdl:Activity" />
		</workflow>
		
	</xsl:template>
		




	<!--regra para gerar todas as connections do vistrails  -->
	<xsl:template match="xpdl:Transition">
		<connection >
			<xsl:attribute name="id"><xsl:value-of select="@Id"></xsl:value-of> </xsl:attribute>
			
			<port type="destination" >
				<xsl:attribute name="id"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='DestinationId']/@Value"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="moduleId"><xsl:value-of select="@To"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="moduleName"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='DestinationModuleName']/@Value"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="name"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='DestinationName']/@Value"></xsl:value-of> </xsl:attribute>	
				<xsl:attribute name="signature"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='DestinationSignature']/@Value"></xsl:value-of> </xsl:attribute>	
			</port>
			
   		    <port type="source" >
   		    	<xsl:attribute name="id"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='SourceId']/@Value"></xsl:value-of> </xsl:attribute>
   		    	<xsl:attribute name="moduleId"><xsl:value-of select="@From"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="moduleName"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='SourceModuleName']/@Value"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="name"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='SourceName']/@Value"></xsl:value-of> </xsl:attribute>	
				<xsl:attribute name="signature"><xsl:value-of select="./xpdl:ExtendedAttributes/xpdl:ExtendedAttribute[@Name='SourceSignature']/@Value"></xsl:value-of> </xsl:attribute>	
   		    </port>	
		</connection>
	</xsl:template>
	

	<!--regra pra gerar os modules do vistrails -->
	<xsl:template match="xpdl:Activity[@grupo5:tipo='module']">
		<module >
			<xsl:attribute name="cache"><xsl:value-of select="@grupo5:cache"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="id"><xsl:value-of select="@Id"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="name"><xsl:value-of select="@Name"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="namespace"><xsl:value-of select="@grupo5:namespace"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="package"><xsl:value-of select="@grupo5:package"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="tag"><xsl:value-of select="@grupo5:tag"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="version"><xsl:value-of select="@grupo5:version"></xsl:value-of> </xsl:attribute>	
			
			
    		<location> 
    			<xsl:attribute name="id"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@grupo5:id"></xsl:value-of> </xsl:attribute>		
    			<xsl:attribute name="x"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@XCoordinate"></xsl:value-of> </xsl:attribute>		
    			<xsl:attribute name="y"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@YCoordinate"></xsl:value-of> </xsl:attribute>		
    		</location>	    		
    		<xsl:apply-templates select="xpdl:Assignments/xpdl:Assignment" />
    		
    		<xsl:apply-templates select=".//xpdl:Input"></xsl:apply-templates>
			<xsl:apply-templates select=".//xpdl:Output"></xsl:apply-templates>    	
			<xsl:apply-templates select="xpdl:Description"></xsl:apply-templates>
    		
    		
  		</module>
	</xsl:template>
	
	<xsl:template match="xpdl:Activity[@grupo5:tipo='abstraction']">
		<abstraction>
			<xsl:attribute name="id"><xsl:value-of select="./@Id"></xsl:value-of> </xsl:attribute>	
			<xsl:attribute name="name"><xsl:value-of select="./@Name"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="cache"><xsl:value-of select="./@grupo5:cache"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="namespace"><xsl:value-of select="./@grupo5:namespace"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="package"><xsl:value-of select="./@grupo5:package"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="version"><xsl:value-of select="./@grupo5:version"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="tag"><xsl:value-of select="./@grupo5:tag"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="intenalVersion"><xsl:value-of select="./@grupo5:internalVersion"></xsl:value-of> </xsl:attribute>
			
			<anotation>
				<xsl:attribute name="key"><xsl:value-of select="__desc__"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="value"><xsl:value-of select="xpdl:Description/text()"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="id"><xsl:number/></xsl:attribute>
			</anotation>
			
			<function>
				<xsl:attribute name="id"><xsl:value-of select="./xpdl:Assignment/@grupo5:id"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="pos"><xsl:value-of select="./xpdl:Assignment/@grupo5:pos"></xsl:value-of> </xsl:attribute>
				<xsl:attribute name="name"><xsl:value-of select="./xpdl:Assignment/@grupo5:name"></xsl:value-of> </xsl:attribute>
				<xsl:apply-templates select=".//grupo5:Parametro"></xsl:apply-templates>
			</function>
		
			
			
		</abstraction>
	</xsl:template>
	
	
	
	<!--regra pra gerar o group do vistrails -->
	<xsl:template match="xpdl:Activity[@grupo5:tipo='group']">
		<group >
					<xsl:attribute name="cache"><xsl:value-of select="@grupo5:cache"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="id"><xsl:value-of select="@Id"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="name"><xsl:value-of select="@Name"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="namespace"><xsl:value-of select="@grupo5:namespace"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="package"><xsl:value-of select="@grupo5:package"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="tag"><xsl:value-of select="@grupo5:tag"></xsl:value-of> </xsl:attribute>	
					<xsl:attribute name="version"><xsl:value-of select="@grupo5:version"></xsl:value-of> </xsl:attribute>	

			<xsl:call-template name="groupWorkflow">
			</xsl:call-template>
				
			
    		<location> 
    			<xsl:attribute name="id"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@grupo5:id"></xsl:value-of> </xsl:attribute>		
    			<xsl:attribute name="x"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@XCoordinate"></xsl:value-of> </xsl:attribute>		
    			<xsl:attribute name="y"><xsl:value-of select="./xpdl:NodeGraphicsInfos/xpdl:NodeGraphicsInfo/xpdl:Coordinates/@YCoordinate"></xsl:value-of> </xsl:attribute>		
    		</location>	   
    		
    		<xsl:apply-templates select=".//xpdl:Assignment"/>
    		<xsl:apply-templates select="xpdl:Description"></xsl:apply-templates>
  		</group>
	</xsl:template>
	
	<xsl:template match="xpdl:Assignment[@grupo5:id]">
		<function>
			<xsl:attribute name="id"><xsl:value-of select="@grupo5:id"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="pos"><xsl:value-of select="@grupo5:pos"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="name"><xsl:value-of select="@grupo5:name"></xsl:value-of> </xsl:attribute>
			<xsl:apply-templates select=".//grupo5:Parametro"></xsl:apply-templates>
			
			
		</function>
	</xsl:template>
	
	<xsl:template match="grupo5:Parametro">
		<parameter>
			<xsl:attribute name="id"><xsl:value-of select="@id"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="pos"><xsl:value-of select="@pos"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="name"><xsl:value-of select="@name"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="type"><xsl:value-of select="@type"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="val"><xsl:value-of select="@val"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="alias"><xsl:value-of select="@alias"></xsl:value-of> </xsl:attribute>
		</parameter>
	</xsl:template>
	
	<xsl:template match="xpdl:Input[@ArtifactId!=999999999]">
		<portSpec>
			<xsl:attribute name="id"><xsl:value-of select="@ArtifactId"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="name"><xsl:value-of select="@grupo5:name"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="type"><xsl:value-of select="@grupo5:type"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="optional"><xsl:value-of select="@grupo5:optional"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="sortKey"><xsl:value-of select="@grupo5:sortKey"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="sigstring"><xsl:value-of select="@grupo5:sigstring"></xsl:value-of> </xsl:attribute>
		</portSpec>
	</xsl:template>
	<xsl:template match="xpdl:Output[@ArtifactId!=999999999]">
		<portSpec>
			<xsl:attribute name="id"><xsl:value-of select="@ArtifactId"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="name"><xsl:value-of select="@grupo5:name"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="type"><xsl:value-of select="@grupo5:type"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="optional"><xsl:value-of select="@grupo5:optional"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="sortKey"><xsl:value-of select="@grupo5:sortKey"></xsl:value-of> </xsl:attribute>
			<xsl:attribute name="sigstring"><xsl:value-of select="@grupo5:sigstring"></xsl:value-of> </xsl:attribute>
		</portSpec>
	</xsl:template>
	
	<xsl:template match="xpdl:Description">
		<annotation key='__desc__'>
			<xsl:attribute name="value"><xsl:value-of select="text()"></xsl:value-of> </xsl:attribute>
		</annotation>
	</xsl:template>
	
	
	<xsl:template match="xpdl:ExtendedAttribute[@Name]">
		<plugin_data>
			<xsl:attribute name="id"><xsl:value-of select="@Name"/></xsl:attribute>
			<xsl:attribute name="data"><xsl:value-of select="@Value"/></xsl:attribute>
		</plugin_data>		
	</xsl:template>
</xsl:stylesheet>